package Foundation;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import TechnicalServices.DataConnector;
import TechnicalServices.IMapper;
import DomainPackage.*;
public class KargoUcakMapper implements IMapper {

	@Override
	public Object get(int ID) {

		KargoUcak kargoucak = new KargoUcak();
		ResultSet rs = DataConnector.getInstance().query("select * from kargoucak where ucakid = " + ID + "");
		
		if (rs != null){
            
            try {
            		rs.next();
            		kargoucak.setUcakID(Integer.parseInt(rs.getString(1)));
            		kargoucak.setUcakAdi(rs.getString(2));
            		kargoucak.setKargoKapasitesi(Float.valueOf(rs.getString(3)));
            		kargoucak.setMenzil(Float.valueOf(rs.getString(4)));
            		kargoucak.setAgirlik(Float.valueOf(rs.getString(5)));
            		kargoucak.setUzunluk(Integer.parseInt(rs.getString(6)));
            		kargoucak.setMaksimumHiz(Float.valueOf(rs.getString(7)));
            		
                             return kargoucak;
                   
            } catch (NumberFormatException e) {
                    System.out.println("KargoUcakMapper: number format problem");
                    e.printStackTrace();
            } catch (SQLException e) {
                    System.out.println("KargoUcakMapper: sql problem");
                    e.printStackTrace();
            }
            
            
    }
    else
            System.out.println("no kargoucak with given ID");

		
		return null;
	}

	@Override
	public int put(Object object) {

		KargoUcak kargoucak=(KargoUcak) object;

			if(!DataConnector.getInstance().update("INSERT INTO kargoucak "+
					"(ucakid, ucakadi, kargokapasitesi, menzil, agirlik, uzunluk, maksimumhiz) " +
					"values ('"+kargoucak.getUcakID()+"', '"+
					kargoucak.getUcakAdi()+"', '"+
					kargoucak.getKargoKapasitesi()+"', '"+
					kargoucak.getMenzil()+"', '"+
					kargoucak.getAgirlik()+"', '"+
					kargoucak.getUzunluk()+"', '"+
					kargoucak.getMaksimumHiz()+"')")){
				System.out.println("error");
			}	
       
		return 0;
	}

	@Override
	public List<Object> getAll() {
		List<Object> allKargoUcakList = new ArrayList<Object>();
    	ResultSet rs = DataConnector.getInstance().query("select * from kargoucak;");
    	int numberOfRows=0;
		try {
			ResultSetMetaData rsMetaData = rs.getMetaData();
            numberOfRows = rsMetaData.getColumnCount()+1;
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
    try {
    	if (rs != null){
            while(rs.next()){
                    
                    KargoUcak kargoucak= new KargoUcak();
                    	kargoucak.setUcakID(Integer.parseInt(rs.getString(1)));
                		kargoucak.setUcakAdi(rs.getString(2));
                		kargoucak.setKargoKapasitesi(Float.valueOf(rs.getString(3)));
                		kargoucak.setMenzil(Float.valueOf(rs.getString(4)));
                		kargoucak.setAgirlik(Float.valueOf(rs.getString(5)));
                		kargoucak.setUzunluk(Integer.parseInt(rs.getString(6)));
                		kargoucak.setMaksimumHiz(Float.valueOf(rs.getString(7)));
                            allKargoUcakList.add(kargoucak);               
            }
    	}
    }
    catch(Exception e){
    	e.printStackTrace();
    }
    	return allKargoUcakList;
			
	}

}
